
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>扩展 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../0/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="21_Protocols.html" />
    
    
    <link rel="prev" href="19_Nested_Types.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    欢迎使用 Swift
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/01_about_swift.html">
            
                <a href="../chapter1/01_about_swift.html">
            
                    
                    关于 Swift
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/02_version_compatibility.html">
            
                <a href="../chapter1/02_version_compatibility.html">
            
                    
                    版本兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/03_a_swift_tour.html">
            
                <a href="../chapter1/03_a_swift_tour.html">
            
                    
                    Swift 初见
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/04_revision_history.html">
            
                <a href="../chapter1/04_revision_history.html">
            
                    
                    Swift 版本历史记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    Swift 教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="01_The_Basics.html">
            
                <a href="01_The_Basics.html">
            
                    
                    基础部分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="02_Basic_Operators.html">
            
                <a href="02_Basic_Operators.html">
            
                    
                    基本运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="03_Strings_and_Characters.html">
            
                <a href="03_Strings_and_Characters.html">
            
                    
                    字符串和字符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="04_Collection_Types.html">
            
                <a href="04_Collection_Types.html">
            
                    
                    集合类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="05_Control_Flow.html">
            
                <a href="05_Control_Flow.html">
            
                    
                    控制流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="06_Functions.html">
            
                <a href="06_Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="07_Closures.html">
            
                <a href="07_Closures.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="08_Enumerations.html">
            
                <a href="08_Enumerations.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="09_Structures_And_Classes.html">
            
                <a href="09_Structures_And_Classes.html">
            
                    
                    类和结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="10_Properties.html">
            
                <a href="10_Properties.html">
            
                    
                    属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="11_Methods.html">
            
                <a href="11_Methods.html">
            
                    
                    方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="12_Subscripts.html">
            
                <a href="12_Subscripts.html">
            
                    
                    下标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="13_Inheritance.html">
            
                <a href="13_Inheritance.html">
            
                    
                    继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="14_Initialization.html">
            
                <a href="14_Initialization.html">
            
                    
                    构造过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="15_Deinitialization.html">
            
                <a href="15_Deinitialization.html">
            
                    
                    析构过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="16_Optional_Chaining.html">
            
                <a href="16_Optional_Chaining.html">
            
                    
                    可选链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="17_Error_Handling.html">
            
                <a href="17_Error_Handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="18_Type_Casting.html">
            
                <a href="18_Type_Casting.html">
            
                    
                    类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="19_Nested_Types.html">
            
                <a href="19_Nested_Types.html">
            
                    
                    嵌套类型
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.20" data-path="20_Extensions.html">
            
                <a href="20_Extensions.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="21_Protocols.html">
            
                <a href="21_Protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="22_Generics.html">
            
                <a href="22_Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="23_Automatic_Reference_Counting.html">
            
                <a href="23_Automatic_Reference_Counting.html">
            
                    
                    自动引用计数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="24_Memory_Safety.html">
            
                <a href="24_Memory_Safety.html">
            
                    
                    内存安全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.25" data-path="25_Access_Control.html">
            
                <a href="25_Access_Control.html">
            
                    
                    访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.26" data-path="26_Advanced_Operators.html">
            
                <a href="26_Advanced_Operators.html">
            
                    
                    高级运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    语言参考
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/01_About_the_Language_Reference.html">
            
                <a href="../chapter3/01_About_the_Language_Reference.html">
            
                    
                    关于语言参考
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/02_Lexical_Structure.html">
            
                <a href="../chapter3/02_Lexical_Structure.html">
            
                    
                    词法结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/03_Types.html">
            
                <a href="../chapter3/03_Types.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter3/04_Expressions.html">
            
                <a href="../chapter3/04_Expressions.html">
            
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter3/05_Statements.html">
            
                <a href="../chapter3/05_Statements.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter3/06_Declarations.html">
            
                <a href="../chapter3/06_Declarations.html">
            
                    
                    声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter3/07_Attributes.html">
            
                <a href="../chapter3/07_Attributes.html">
            
                    
                    特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter3/08_Patterns.html">
            
                <a href="../chapter3/08_Patterns.html">
            
                    
                    模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                <a href="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                    
                    泛型参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter3/10_Summary_of_the_Grammar.html">
            
                <a href="../chapter3/10_Summary_of_the_Grammar.html">
            
                    
                    语法总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    翻译贡献者
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../contributors.html">
            
                <a href="../contributors.html">
            
                    
                    翻译贡献者
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >扩展</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6269;&#x5C55;">&#x6269;&#x5C55;</h1>
<p><em>&#x6269;&#x5C55;</em>&#x53EF;&#x4EE5;&#x7ED9;&#x4E00;&#x4E2A;&#x73B0;&#x6709;&#x7684;&#x7C7B;&#xFF0C;&#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x679A;&#x4E3E;&#xFF0C;&#x8FD8;&#x6709;&#x534F;&#x8BAE;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x529F;&#x80FD;&#x3002;&#x5B83;&#x8FD8;&#x62E5;&#x6709;&#x4E0D;&#x9700;&#x8981;&#x8BBF;&#x95EE;&#x88AB;&#x6269;&#x5C55;&#x7C7B;&#x578B;&#x6E90;&#x4EE3;&#x7801;&#x5C31;&#x80FD;&#x5B8C;&#x6210;&#x6269;&#x5C55;&#x7684;&#x80FD;&#x529B;&#xFF08;&#x5373;<em>&#x9006;&#x5411;&#x5EFA;&#x6A21;</em>&#xFF09;&#x3002;&#x6269;&#x5C55;&#x548C; Objective-C &#x7684;&#x5206;&#x7C7B;&#x5F88;&#x76F8;&#x4F3C;&#x3002;&#xFF08;&#x4E0E; Objective-C &#x5206;&#x7C7B;&#x4E0D;&#x540C;&#x7684;&#x662F;&#xFF0C;Swift &#x6269;&#x5C55;&#x662F;&#x6CA1;&#x6709;&#x540D;&#x5B57;&#x7684;&#x3002;&#xFF09;</p>
<p>Swift &#x4E2D;&#x7684;&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#xFF1A;</p>
<ul>
<li>&#x6DFB;&#x52A0;&#x8BA1;&#x7B97;&#x578B;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#x548C;&#x8BA1;&#x7B97;&#x578B;&#x7C7B;&#x5C5E;&#x6027;</li>
<li>&#x5B9A;&#x4E49;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x548C;&#x7C7B;&#x65B9;&#x6CD5;</li>
<li>&#x63D0;&#x4F9B;&#x65B0;&#x7684;&#x6784;&#x9020;&#x5668;</li>
<li>&#x5B9A;&#x4E49;&#x4E0B;&#x6807;</li>
<li>&#x5B9A;&#x4E49;&#x548C;&#x4F7F;&#x7528;&#x65B0;&#x7684;&#x5D4C;&#x5957;&#x7C7B;&#x578B;</li>
<li>&#x4F7F;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684;&#x7C7B;&#x578B;&#x9075;&#x5FAA;&#xFF08;conform&#xFF09;&#x4E00;&#x4E2A;&#x534F;&#x8BAE;</li>
</ul>
<p>&#x5728; Swift &#x4E2D;&#xFF0C;&#x4F60;&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x6269;&#x5C55;&#x534F;&#x8BAE;&#x4EE5;&#x63D0;&#x4F9B;&#x5176;&#x9700;&#x8981;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x6216;&#x8005;&#x6DFB;&#x52A0;&#x989D;&#x5916;&#x529F;&#x80FD;&#x7ED9;&#x9075;&#x5FAA;&#x7684;&#x7C7B;&#x578B;&#x6240;&#x4F7F;&#x7528;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x4ECE; <a href="https://docs.swift.org/swift-book/LanguageGuide/Protocols.html#ID521" target="_blank">&#x534F;&#x8BAE;&#x6269;&#x5C55;</a> &#x83B7;&#x53D6;&#x66F4;&#x591A;&#x7EC6;&#x8282;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x7ED9;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x80FD;&#x91CD;&#x5199;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684;&#x529F;&#x80FD;&#x3002;</p>
</blockquote>
<h2 id="extension-syntax">&#x6269;&#x5C55;&#x7684;&#x8BED;&#x6CD5; </h2>
<p>&#x4F7F;&#x7528; <code>extension</code> &#x5173;&#x952E;&#x5B57;&#x58F0;&#x660E;&#x6269;&#x5C55;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">SomeType</span> </span>{
  <span class="hljs-comment">// &#x5728;&#x8FD9;&#x91CC;&#x7ED9; SomeType &#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x529F;&#x80FD;</span>
}
</code></pre>
<p>&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x6269;&#x5145;&#x4E00;&#x4E2A;&#x73B0;&#x6709;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x7ED9;&#x5B83;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x534F;&#x8BAE;&#x3002;&#x534F;&#x8BAE;&#x540D;&#x79F0;&#x7684;&#x5199;&#x6CD5;&#x548C;&#x7C7B;&#x6216;&#x8005;&#x7ED3;&#x6784;&#x4F53;&#x4E00;&#x6837;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">SomeType</span>: <span class="hljs-title">SomeProtocol</span>, <span class="hljs-title">AnotherProtocol</span> </span>{
  <span class="hljs-comment">// &#x534F;&#x8BAE;&#x6240;&#x9700;&#x8981;&#x7684;&#x5B9E;&#x73B0;&#x5199;&#x5728;&#x8FD9;&#x91CC;</span>
}
</code></pre>
<p>&#x8FD9;&#x79CD;&#x9075;&#x5FAA;&#x534F;&#x8BAE;&#x7684;&#x65B9;&#x5F0F;&#x5728; <a href="https://docs.swift.org/swift-book/LanguageGuide/Protocols.html#ID277" target="_blank">&#x4F7F;&#x7528;&#x6269;&#x5C55;&#x9075;&#x5FAA;&#x534F;&#x8BAE;</a> &#x4E2D;&#x6709;&#x63CF;&#x8FF0;&#x3002;</p>
<p>&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5728;&#x73B0;&#x6709;&#x8303;&#x578B;&#x7C7B;&#x578B;&#x4E0A;&#xFF0C;&#x5C31;&#x50CF; <a href="https://docs.swift.org/swift-book/LanguageGuide/Generics.html#ID185" target="_blank">&#x6269;&#x5C55;&#x8303;&#x578B;&#x7C7B;&#x578B;</a> &#x4E2D;&#x63CF;&#x8FF0;&#x7684;&#x4E00;&#x6837;&#x3002;&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x6269;&#x5C55;&#x7ED9;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x6709;&#x6761;&#x4EF6;&#x7684;&#x6DFB;&#x52A0;&#x529F;&#x80FD;&#xFF0C;&#x5C31;&#x50CF; <a href="https://docs.swift.org/swift-book/LanguageGuide/Generics.html#ID553" target="_blank">&#x6269;&#x5C55;&#x4E00;&#x4E2A;&#x5E26;&#x6709; Where &#x5B57;&#x53E5;&#x7684;&#x8303;&#x578B;</a> &#x4E2D;&#x63CF;&#x8FF0;&#x7684;&#x4E00;&#x6837;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x5BF9;&#x4E00;&#x4E2A;&#x73B0;&#x6709;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x6269;&#x5C55;&#x6765;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x6240;&#x6709;&#x5B9E;&#x4F8B;&#x90FD;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x65B0;&#x529F;&#x80FD;&#xFF0C;&#x5305;&#x62EC;&#x90A3;&#x4E9B;&#x5728;&#x6269;&#x5C55;&#x5B9A;&#x4E49;&#x4E4B;&#x524D;&#x5C31;&#x5B58;&#x5728;&#x7684;&#x5B9E;&#x4F8B;&#x3002;</p>
</blockquote>
<h2 id="computed-properties">&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027; </h2>
<p>&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x7ED9;&#x73B0;&#x6709;&#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x8BA1;&#x7B97;&#x578B;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#x548C;&#x8BA1;&#x7B97;&#x578B;&#x7C7B;&#x5C5E;&#x6027;&#x3002;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x7ED9; Swift &#x5185;&#x5EFA;&#x7684; <code>Double</code> &#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x4E86;&#x4E94;&#x4E2A;&#x8BA1;&#x7B97;&#x578B;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#xFF0C;&#x4ECE;&#x800C;&#x63D0;&#x4F9B;&#x4E0E;&#x8DDD;&#x79BB;&#x5355;&#x4F4D;&#x76F8;&#x5173;&#x5DE5;&#x4F5C;&#x7684;&#x57FA;&#x672C;&#x652F;&#x6301;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">Double</span> </span>{
    <span class="hljs-keyword">var</span> km: <span class="hljs-type">Double</span> { <span class="hljs-keyword">return</span> <span class="hljs-keyword">self</span> * <span class="hljs-number">1_000.0</span> }
    <span class="hljs-keyword">var</span> m: <span class="hljs-type">Double</span> { <span class="hljs-keyword">return</span> <span class="hljs-keyword">self</span> }
    <span class="hljs-keyword">var</span> cm: <span class="hljs-type">Double</span> { <span class="hljs-keyword">return</span> <span class="hljs-keyword">self</span> / <span class="hljs-number">100.0</span> }
    <span class="hljs-keyword">var</span> mm: <span class="hljs-type">Double</span> { <span class="hljs-keyword">return</span> <span class="hljs-keyword">self</span> / <span class="hljs-number">1_000.0</span> }
    <span class="hljs-keyword">var</span> ft: <span class="hljs-type">Double</span> { <span class="hljs-keyword">return</span> <span class="hljs-keyword">self</span> / <span class="hljs-number">3.28084</span> }
}
<span class="hljs-keyword">let</span> oneInch = <span class="hljs-number">25.4</span>.mm
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;One inch is <span class="hljs-subst">\(oneInch)</span> meters&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;One inch is 0.0254 meters&#x201D;</span>
<span class="hljs-keyword">let</span> threeFeet = <span class="hljs-number">3</span>.ft
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Three feet is <span class="hljs-subst">\(threeFeet)</span> meters&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Three feet is 0.914399970739201 meters&#x201D;</span>
</code></pre>
<p>&#x8FD9;&#x4E9B;&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027;&#x8868;&#x793A;&#x7684;&#x542B;&#x4E49;&#x662F;&#x628A;&#x4E00;&#x4E2A; <code>Double</code> &#x503C;&#x770B;&#x4F5C;&#x662F;&#x67D0;&#x5355;&#x4F4D;&#x4E0B;&#x7684;&#x957F;&#x5EA6;&#x503C;&#x3002;&#x5373;&#x4F7F;&#x5B83;&#x4EEC;&#x88AB;&#x5B9E;&#x73B0;&#x4E3A;&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027;&#xFF0C;&#x4F46;&#x8FD9;&#x4E9B;&#x5C5E;&#x6027;&#x7684;&#x540D;&#x5B57;&#x4ECD;&#x53EF;&#x7D27;&#x63A5;&#x4E00;&#x4E2A;&#x6D6E;&#x70B9;&#x578B;&#x5B57;&#x9762;&#x503C;&#xFF0C;&#x4ECE;&#x800C;&#x901A;&#x8FC7;&#x70B9;&#x8BED;&#x6CD5;&#x6765;&#x4F7F;&#x7528;&#xFF0C;&#x5E76;&#x4EE5;&#x6B64;&#x5B9E;&#x73B0;&#x8DDD;&#x79BB;&#x8F6C;&#x6362;&#x3002;</p>
<p>&#x5728;&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>Double</code> &#x7C7B;&#x578B;&#x7684; <code>1.0</code> &#x4EE3;&#x8868;&#x7684;&#x662F;&#x201C;&#x4E00;&#x7C73;&#x201D;&#x3002;&#x8FD9;&#x5C31;&#x662F;&#x4E3A;&#x4EC0;&#x4E48;&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027; <code>m</code> &#x8FD4;&#x56DE;&#x7684;&#x662F; <code>self</code>&#x2014;&#x2014;&#x8868;&#x8FBE;&#x5F0F; <code>1.m</code> &#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x8BA1;&#x7B97;&#x4E00;&#x4E2A; <code>Double</code> &#x7C7B;&#x578B;&#x7684; <code>1.0</code>&#x3002;</p>
<p>&#x5176;&#x5B83;&#x5355;&#x4F4D;&#x5219;&#x9700;&#x8981;&#x4E00;&#x4E9B;&#x5355;&#x4F4D;&#x6362;&#x7B97;&#x3002;&#x4E00;&#x5343;&#x7C73;&#x7B49;&#x4E8E; 1,000 &#x7C73;&#xFF0C;&#x6240;&#x4EE5;&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027; <code>km</code> &#x8981;&#x628A;&#x503C;&#x4E58;&#x4EE5; <code>1_000.00</code> &#x6765;&#x5B9E;&#x73B0;&#x5343;&#x7C73;&#x5230;&#x7C73;&#x7684;&#x5355;&#x4F4D;&#x6362;&#x7B97;&#x3002;&#x7C7B;&#x4F3C;&#x5730;&#xFF0C;&#x4E00;&#x7C73;&#x6709; 3.28084 &#x82F1;&#x5C3A;&#xFF0C;&#x6240;&#x4EE5;&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027; <code>ft</code> &#x8981;&#x628A;&#x5BF9;&#x5E94;&#x7684; <code>Double</code> &#x503C;&#x9664;&#x4EE5; <code>3.28084</code>&#xFF0C;&#x6765;&#x5B9E;&#x73B0;&#x82F1;&#x5C3A;&#x5230;&#x7C73;&#x7684;&#x5355;&#x4F4D;&#x6362;&#x7B97;&#x3002;</p>
<p>&#x8FD9;&#x4E9B;&#x5C5E;&#x6027;&#x90FD;&#x662F;&#x53EA;&#x8BFB;&#x7684;&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027;&#xFF0C;&#x6240;&#x4EE5;&#x4E3A;&#x4E86;&#x7B80;&#x4FBF;&#xFF0C;&#x5B83;&#x4EEC;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x91CC;&#x9762;&#x90FD;&#x4E0D;&#x5305;&#x542B; <code>get</code> &#x5173;&#x952E;&#x5B57;&#x3002;&#x5B83;&#x4EEC;&#x4F7F;&#x7528; <code>Double</code> &#x4F5C;&#x4E3A;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#xFF0C;&#x5E76;&#x53EF;&#x7528;&#x4E8E;&#x6240;&#x6709;&#x63A5;&#x53D7; <code>Double</code> &#x7C7B;&#x578B;&#x7684;&#x6570;&#x5B66;&#x8BA1;&#x7B97;&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> aMarathon = <span class="hljs-number">42</span>.km + <span class="hljs-number">195</span>.m
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;A marathon is <span class="hljs-subst">\(aMarathon)</span> meters long&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;A marathon is 42195.0 meters long&#x201D;</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x8BA1;&#x7B97;&#x5C5E;&#x6027;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x4EEC;&#x4E0D;&#x80FD;&#x6DFB;&#x52A0;&#x5B58;&#x50A8;&#x5C5E;&#x6027;&#xFF0C;&#x6216;&#x5411;&#x73B0;&#x6709;&#x7684;&#x5C5E;&#x6027;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;&#x89C2;&#x5BDF;&#x8005;&#x3002;</p>
</blockquote>
<h2 id="initializers">&#x6784;&#x9020;&#x5668; </h2>
<p>&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x7ED9;&#x73B0;&#x6709;&#x7684;&#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x6784;&#x9020;&#x5668;&#x3002;&#x5B83;&#x4F7F;&#x4F60;&#x53EF;&#x4EE5;&#x628A;&#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x6765;&#x4F9B;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x7684;&#x6784;&#x9020;&#x5668;&#x4F7F;&#x7528;&#xFF0C;&#x6216;&#x8005;&#x5728;&#x7C7B;&#x578B;&#x7684;&#x539F;&#x59CB;&#x5B9E;&#x73B0;&#x4E0A;&#x6DFB;&#x52A0;&#x989D;&#x5916;&#x7684;&#x6784;&#x9020;&#x9009;&#x9879;&#x3002;</p>
<p>&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x7ED9;&#x4E00;&#x4E2A;&#x7C7B;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x4FBF;&#x5229;&#x6784;&#x9020;&#x5668;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x4EEC;&#x4E0D;&#x80FD;&#x7ED9;&#x7C7B;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x6307;&#x5B9A;&#x6784;&#x9020;&#x5668;&#x6216;&#x8005;&#x6790;&#x6784;&#x5668;&#x3002;&#x6307;&#x5B9A;&#x6784;&#x9020;&#x5668;&#x548C;&#x6790;&#x6784;&#x5668;&#x5FC5;&#x987B;&#x59CB;&#x7EC8;&#x7531;&#x7C7B;&#x7684;&#x539F;&#x59CB;&#x5B9E;&#x73B0;&#x63D0;&#x4F9B;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x6269;&#x5C55;&#x7ED9;&#x4E00;&#x4E2A;&#x503C;&#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x6784;&#x9020;&#x5668;&#x53EA;&#x662F;&#x7528;&#x4E8E;&#x7ED9;&#x6240;&#x6709;&#x7684;&#x5B58;&#x50A8;&#x5C5E;&#x6027;&#x63D0;&#x4F9B;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x5E76;&#x4E14;&#x6CA1;&#x6709;&#x5B9A;&#x4E49;&#x4EFB;&#x4F55;&#x81EA;&#x5B9A;&#x4E49;&#x6784;&#x9020;&#x5668;&#xFF0C;&#x90A3;&#x4E48;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x8BE5;&#x503C;&#x7C7B;&#x578B;&#x6269;&#x5C55;&#x7684;&#x6784;&#x9020;&#x5668;&#x4E2D;&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x6784;&#x9020;&#x5668;&#x548C;&#x6210;&#x5458;&#x6784;&#x9020;&#x5668;&#x3002;&#x5982;&#x679C;&#x4F60;&#x628A;&#x6784;&#x9020;&#x5668;&#x5199;&#x5230;&#x4E86;&#x503C;&#x7C7B;&#x578B;&#x7684;&#x539F;&#x59CB;&#x5B9E;&#x73B0;&#x4E2D;&#xFF0C;&#x5C31;&#x50CF; <a href="https://docs.swift.org/swift-book/LanguageGuide/Initialization.html#ID215" target="_blank">&#x503C;&#x7C7B;&#x578B;&#x7684;&#x6784;&#x9020;&#x5668;&#x59D4;&#x6258;</a> &#x4E2D;&#x6240;&#x63CF;&#x8FF0;&#x7684;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x4E0D;&#x5C5E;&#x4E8E;&#x5728;&#x6269;&#x5C55;&#x4E2D;&#x6DFB;&#x52A0;&#x6784;&#x9020;&#x5668;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x6269;&#x5C55;&#x7ED9;&#x53E6;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#x6DFB;&#x52A0;&#x6784;&#x9020;&#x5668;&#xFF0C;&#x90A3;&#x4E48;&#x65B0;&#x7684;&#x6784;&#x9020;&#x5668;&#x76F4;&#x5230;&#x5B9A;&#x4E49;&#x6A21;&#x5757;&#x4E2D;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x5668;&#x4E4B;&#x524D;&#xFF0C;&#x4E0D;&#x80FD;&#x8BBF;&#x95EE; <code>self</code>&#x3002;</p>
<p>&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x81EA;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x7684; <code>Rect</code> &#x7ED3;&#x6784;&#x4F53;&#x7528;&#x6765;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x51E0;&#x4F55;&#x77E9;&#x5F62;&#x3002;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#x8FD8;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x7ED9;&#x4E88;&#x652F;&#x6301;&#x7684;&#x7ED3;&#x6784;&#x4F53; <code>Size</code> &#x548C; <code>Point</code>&#xFF0C;&#x5B83;&#x4EEC;&#x90FD;&#x628A;&#x5C5E;&#x6027;&#x7684;&#x9ED8;&#x8BA4;&#x503C;&#x8BBE;&#x7F6E;&#x4E3A; <code>0.0</code>&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Size</span> </span>{
    <span class="hljs-keyword">var</span> width = <span class="hljs-number">0.0</span>, height = <span class="hljs-number">0.0</span>
}
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Point</span> </span>{
    <span class="hljs-keyword">var</span> x = <span class="hljs-number">0.0</span>, y = <span class="hljs-number">0.0</span>
}
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Rect</span> </span>{
    <span class="hljs-keyword">var</span> origin = <span class="hljs-type">Point</span>()
    <span class="hljs-keyword">var</span> size = <span class="hljs-type">Size</span>()
}
</code></pre>
<p>&#x56E0;&#x4E3A; <code>Rect</code> &#x7ED3;&#x6784;&#x4F53;&#x7ED9;&#x6240;&#x6709;&#x7684;&#x5C5E;&#x6027;&#x90FD;&#x63D0;&#x4F9B;&#x4E86;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x81EA;&#x52A8;&#x83B7;&#x5F97;&#x4E86;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x6784;&#x9020;&#x5668;&#x548C;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#x6784;&#x9020;&#x5668;&#xFF0C;&#x5C31;&#x50CF; <a href="https://docs.swift.org/swift-book/LanguageGuide/Initialization.html#ID213" target="_blank">&#x9ED8;&#x8BA4;&#x6784;&#x9020;&#x5668;</a> &#x4E2D;&#x63CF;&#x8FF0;&#x7684;&#x4E00;&#x6837;&#x3002;&#x8FD9;&#x4E9B;&#x6784;&#x9020;&#x5668;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x65B0;&#x7684; <code>Rect</code> &#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> defaultRect = <span class="hljs-type">Rect</span>()
<span class="hljs-keyword">let</span> memberwiseRect = <span class="hljs-type">Rect</span>(origin: <span class="hljs-type">Point</span>(x: <span class="hljs-number">2.0</span>, y: <span class="hljs-number">2.0</span>),
   size: <span class="hljs-type">Size</span>(width: <span class="hljs-number">5.0</span>, height: <span class="hljs-number">5.0</span>))
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6269;&#x5C55; <code>Rect</code> &#x7ED3;&#x6784;&#x4F53;&#x6765;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x5141;&#x8BB8;&#x6307;&#x5B9A; point &#x548C; size &#x7684;&#x6784;&#x9020;&#x5668;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">Rect</span> </span>{
    <span class="hljs-keyword">init</span>(center: <span class="hljs-type">Point</span>, size: <span class="hljs-type">Size</span>) {
        <span class="hljs-keyword">let</span> originX = center.x - (size.width / <span class="hljs-number">2</span>)
        <span class="hljs-keyword">let</span> originY = center.y - (size.height / <span class="hljs-number">2</span>)
        <span class="hljs-keyword">self</span>.<span class="hljs-keyword">init</span>(origin: <span class="hljs-type">Point</span>(x: originX, y: originY), size: size)
    }
}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x65B0;&#x7684;&#x6784;&#x9020;&#x5668;&#x9996;&#x5148;&#x6839;&#x636E;&#x63D0;&#x4F9B;&#x7684; <code>center</code> &#x548C; <code>size</code> &#x8BA1;&#x7B97;&#x4E00;&#x4E2A;&#x9002;&#x5F53;&#x7684;&#x539F;&#x70B9;&#x3002;&#x7136;&#x540E;&#x8FD9;&#x4E2A;&#x6784;&#x9020;&#x5668;&#x8C03;&#x7528;&#x7ED3;&#x6784;&#x4F53;&#x81EA;&#x5E26;&#x7684;&#x6210;&#x5458;&#x6784;&#x9020;&#x5668; <code>init(origin:size:)</code>&#xFF0C;&#x5B83;&#x4F1A;&#x5C06;&#x65B0;&#x7684; origin &#x548C; size &#x503C;&#x50A8;&#x5B58;&#x5728;&#x9002;&#x5F53;&#x7684;&#x5C5E;&#x6027;&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> centerRect = <span class="hljs-type">Rect</span>(center: <span class="hljs-type">Point</span>(x: <span class="hljs-number">4.0</span>, y: <span class="hljs-number">4.0</span>),
                      size: <span class="hljs-type">Size</span>(width: <span class="hljs-number">3.0</span>, height: <span class="hljs-number">3.0</span>))
<span class="hljs-comment">// centerRect &#x7684; origin &#x662F; (2.5, 2.5) &#x5E76;&#x4E14;&#x5B83;&#x7684; size &#x662F; (3.0, 3.0)</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x901A;&#x8FC7;&#x6269;&#x5C55;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6784;&#x9020;&#x5668;&#xFF0C;&#x4F60;&#x6709;&#x8D23;&#x4EFB;&#x786E;&#x4FDD;&#x6BCF;&#x4E2A;&#x901A;&#x8FC7;&#x8BE5;&#x6784;&#x9020;&#x5668;&#x521B;&#x5EFA;&#x7684;&#x5B9E;&#x4F8B;&#x90FD;&#x662F;&#x521D;&#x59CB;&#x5316;&#x5B8C;&#x6574;&#x7684;&#x3002;</p>
</blockquote>
<h2 id="methods">&#x65B9;&#x6CD5; </h2>
<p>&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x7ED9;&#x73B0;&#x6709;&#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x548C;&#x7C7B;&#x65B9;&#x6CD5;&#x3002;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x7ED9; <code>Int</code> &#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x53EB;&#x505A; <code>repetitions</code>&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">Int</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">repetitions</span><span class="hljs-params">(task: <span class="hljs-params">()</span></span></span> -&gt; <span class="hljs-type">Void</span>) {
        <span class="hljs-keyword">for</span> <span class="hljs-number">_</span> <span class="hljs-keyword">in</span> <span class="hljs-number">0</span>..&lt;<span class="hljs-keyword">self</span> {
            task()
        }
    }
}
</code></pre>
<p><code>repetitions(task:)</code> &#x65B9;&#x6CD5;&#x4EC5;&#x63A5;&#x6536;&#x4E00;&#x4E2A; <code>() -&gt; Void</code> &#x7C7B;&#x578B;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x5B83;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x6CA1;&#x6709;&#x53C2;&#x6570;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x5B9A;&#x4E49;&#x4E86;&#x8FD9;&#x4E2A;&#x6269;&#x5C55;&#x4E4B;&#x540E;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5BF9;&#x4EFB;&#x610F;&#x6574;&#x5F62;&#x6570;&#x503C;&#x8C03;&#x7528; <code>repetitions(task:)</code> &#x65B9;&#x6CD5;&#xFF0C;&#x6765;&#x6267;&#x884C;&#x5BF9;&#x5E94;&#x6B21;&#x6570;&#x7684;&#x4EFB;&#x52A1;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-number">3</span>.repetitions {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Hello!&quot;</span>)
}
<span class="hljs-comment">// Hello!</span>
<span class="hljs-comment">// Hello!</span>
<span class="hljs-comment">// Hello!</span>
</code></pre>
<h3 id="mutating-instance-methods">&#x53EF;&#x53D8;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5; </h3>
<p>&#x901A;&#x8FC7;&#x6269;&#x5C55;&#x6DFB;&#x52A0;&#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x540C;&#x6837;&#x4E5F;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#xFF08;&#x6216; <em>mutating&#xFF08;&#x6539;&#x53D8;&#xFF09;</em>&#xFF09;&#x5B9E;&#x4F8B;&#x672C;&#x8EAB;&#x3002;&#x7ED3;&#x6784;&#x4F53;&#x548C;&#x679A;&#x4E3E;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x82E5;&#x662F;&#x53EF;&#x4EE5;&#x4FEE;&#x6539; <code>self</code> &#x6216;&#x8005;&#x5B83;&#x81EA;&#x5DF1;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x5C06;&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x6807;&#x8BB0;&#x4E3A; <code>mutating</code>&#xFF0C;&#x5C31;&#x50CF;&#x662F;&#x6539;&#x53D8;&#x4E86;&#x65B9;&#x6CD5;&#x7684;&#x539F;&#x59CB;&#x5B9E;&#x73B0;&#x3002;</p>
<p>&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x5BF9; Swift &#x7684; <code>Int</code> &#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684; mutating &#x65B9;&#x6CD5;&#xFF0C;&#x53EB;&#x505A; <code>square</code>&#xFF0C;&#x5B83;&#x5C06;&#x539F;&#x59CB;&#x503C;&#x6C42;&#x5E73;&#x65B9;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">Int</span> </span>{
    <span class="hljs-keyword">mutating</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">square</span><span class="hljs-params">()</span></span> {
        <span class="hljs-keyword">self</span> = <span class="hljs-keyword">self</span> * <span class="hljs-keyword">self</span>
    }
}
<span class="hljs-keyword">var</span> someInt = <span class="hljs-number">3</span>
someInt.square()
<span class="hljs-comment">// someInt &#x73B0;&#x5728;&#x662F; 9</span>
</code></pre>
<h2 id="subscripts">&#x4E0B;&#x6807; </h2>
<p>&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x7ED9;&#x73B0;&#x6709;&#x7684;&#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x4E0B;&#x6807;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x5BF9; Swift &#x7684; <code>Int</code> &#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x7C7B;&#x578B;&#x7684;&#x4E0B;&#x6807;&#x3002;&#x4E0B;&#x6807; <code>[n]</code> &#x4ECE;&#x6570;&#x5B57;&#x53F3;&#x4FA7;&#x5F00;&#x59CB;&#xFF0C;&#x8FD4;&#x56DE;&#x5C0F;&#x6570;&#x70B9;&#x540E;&#x7684;&#x7B2C; <code>n</code> &#x4F4D;&#xFF1A;</p>
<ul>
<li><code>123456789[0]</code> &#x8FD4;&#x56DE; <code>9</code></li>
<li><code>123456789[1]</code> &#x8FD4;&#x56DE; <code>8</code></li>
</ul>
<p>&#x2026;&#x2026;&#x4EE5;&#x6B64;&#x7C7B;&#x63A8;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">Int</span> </span>{
    <span class="hljs-keyword">subscript</span>(digitIndex: <span class="hljs-type">Int</span>) -&gt; <span class="hljs-type">Int</span> {
        <span class="hljs-keyword">var</span> decimalBase = <span class="hljs-number">1</span>
        <span class="hljs-keyword">for</span> <span class="hljs-number">_</span> <span class="hljs-keyword">in</span> <span class="hljs-number">0</span>..&lt;digitIndex {
            decimalBase *= <span class="hljs-number">10</span>
        }
        <span class="hljs-keyword">return</span> (<span class="hljs-keyword">self</span> / decimalBase) % <span class="hljs-number">10</span>
    }
}
<span class="hljs-number">746381295</span>[<span class="hljs-number">0</span>]
<span class="hljs-comment">// &#x8FD4;&#x56DE; 5</span>
<span class="hljs-number">746381295</span>[<span class="hljs-number">1</span>]
<span class="hljs-comment">// &#x8FD4;&#x56DE; 9</span>
<span class="hljs-number">746381295</span>[<span class="hljs-number">2</span>]
<span class="hljs-comment">// &#x8FD4;&#x56DE; 2</span>
<span class="hljs-number">746381295</span>[<span class="hljs-number">8</span>]
<span class="hljs-comment">// &#x8FD4;&#x56DE; 7</span>
</code></pre>
<p>&#x5982;&#x679C;&#x64CD;&#x4F5C;&#x7684; <code>Int</code> &#x503C;&#x6CA1;&#x6709;&#x8DB3;&#x591F;&#x7684;&#x4F4D;&#x6570;&#x6EE1;&#x8DB3;&#x6240;&#x8BF7;&#x6C42;&#x7684;&#x4E0B;&#x6807;&#xFF0C;&#x90A3;&#x4E48;&#x4E0B;&#x6807;&#x7684;&#x73B0;&#x5B9E;&#x5C06;&#x8FD4;&#x56DE; <code>0</code>&#xFF0C;&#x5C06;&#x597D;&#x50CF;&#x5728;&#x6570;&#x5B57;&#x7684;&#x5DE6;&#x8FB9;&#x8865;&#x4E0A;&#x4E86; 0&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-number">746381295</span>[<span class="hljs-number">9</span>]
<span class="hljs-comment">// &#x8FD4;&#x56DE; 0&#xFF0C;&#x5C31;&#x597D;&#x50CF;&#x4F60;&#x8FDB;&#x884C;&#x4E86;&#x8FD9;&#x4E2A;&#x8BF7;&#x6C42;&#xFF1A;</span>
<span class="hljs-number">0746381295</span>[<span class="hljs-number">9</span>]
</code></pre>
<h2 id="nested-yypes">&#x5D4C;&#x5957;&#x7C7B;&#x578B; </h2>
<p>&#x6269;&#x5C55;&#x53EF;&#x4EE5;&#x7ED9;&#x73B0;&#x6709;&#x7684;&#x7C7B;&#xFF0C;&#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x8FD8;&#x6709;&#x679A;&#x4E3E;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x5D4C;&#x5957;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">Int</span> </span>{
    <span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">Kind</span> </span>{
        <span class="hljs-keyword">case</span> negative, zero, positive
    }
    <span class="hljs-keyword">var</span> kind: <span class="hljs-type">Kind</span> {
        <span class="hljs-keyword">switch</span> <span class="hljs-keyword">self</span> {
        <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:
            <span class="hljs-keyword">return</span> .zero
        <span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> x <span class="hljs-keyword">where</span> x &gt; <span class="hljs-number">0</span>:
            <span class="hljs-keyword">return</span> .positive
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> .negative
        }
    }
}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x7ED9; <code>Int</code> &#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5D4C;&#x5957;&#x679A;&#x4E3E;&#x3002;&#x8FD9;&#x4E2A;&#x679A;&#x4E3E;&#x53EB;&#x505A; <code>Kind</code>&#xFF0C;&#x8868;&#x793A;&#x7279;&#x5B9A;&#x6574;&#x6570;&#x6240;&#x4EE3;&#x8868;&#x7684;&#x6570;&#x5B57;&#x7C7B;&#x578B;&#x3002;&#x5177;&#x4F53;&#x6765;&#x8BF4;&#xFF0C;&#x5B83;&#x8868;&#x793A;&#x6570;&#x5B57;&#x662F;&#x8D1F;&#x7684;&#x3001;&#x96F6;&#x7684;&#x8FD8;&#x662F;&#x6B63;&#x7684;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x540C;&#x6837;&#x7ED9; <code>Int</code> &#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x8BA1;&#x7B97;&#x578B;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#xFF0C;&#x53EB;&#x505A; <code>kind</code>&#xFF0C;&#x5B83;&#x8FD4;&#x56DE;&#x88AB;&#x64CD;&#x4F5C;&#x6574;&#x6570;&#x6240;&#x5BF9;&#x5E94;&#x7684; <code>Kind</code> &#x679A;&#x4E3E; case &#x5206;&#x652F;&#x3002;</p>
<p>&#x73B0;&#x5728;&#xFF0C;&#x4EFB;&#x610F; <code>Int</code> &#x7684;&#x503C;&#x90FD;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x5D4C;&#x5957;&#x7C7B;&#x578B;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">printIntegerKinds</span><span class="hljs-params">(<span class="hljs-number">_</span> numbers: [Int])</span></span> {
    <span class="hljs-keyword">for</span> number <span class="hljs-keyword">in</span> numbers {
        <span class="hljs-keyword">switch</span> number.kind {
        <span class="hljs-keyword">case</span> .negative:
            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;- &quot;</span>, terminator: <span class="hljs-string">&quot;&quot;</span>)
        <span class="hljs-keyword">case</span> .zero:
            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;0 &quot;</span>, terminator: <span class="hljs-string">&quot;&quot;</span>)
        <span class="hljs-keyword">case</span> .positive:
            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;+ &quot;</span>, terminator: <span class="hljs-string">&quot;&quot;</span>)
        }
    }
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;&quot;</span>)
}
printIntegerKinds([<span class="hljs-number">3</span>, <span class="hljs-number">19</span>, -<span class="hljs-number">27</span>, <span class="hljs-number">0</span>, -<span class="hljs-number">6</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>])
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;+ + - 0 - 0 + &#x201D;</span>
</code></pre>
<p>&#x65B9;&#x6CD5; <code>printIntegerKinds(_:)</code>&#xFF0C;&#x4F7F;&#x7528;&#x4E00;&#x4E2A; <code>Int</code> &#x7C7B;&#x578B;&#x7684;&#x6570;&#x7EC4;&#x4F5C;&#x4E3A;&#x8F93;&#x5165;&#xFF0C;&#x7136;&#x540E;&#x4F9D;&#x6B21;&#x8FED;&#x4EE3;&#x8FD9;&#x4E9B;&#x503C;&#x3002;&#x5BF9;&#x4E8E;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#xFF0C;&#x65B9;&#x6CD5;&#x4F1A;&#x68C0;&#x67E5;&#x5B83;&#x7684; <code>kind</code> &#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027;&#xFF0C;&#x7136;&#x540E;&#x6253;&#x5370;&#x9002;&#x5F53;&#x7684;&#x63CF;&#x8FF0;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p><code>number.kind</code> &#x5DF2;&#x7ECF;&#x88AB;&#x8BA4;&#x4E3A;&#x662F; <code>Int.Kind</code> &#x7C7B;&#x578B;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x5728; <code>switch</code> &#x8BED;&#x53E5;&#x4E2D;&#x6240;&#x6709;&#x7684; <code>Int.Kind</code> case &#x5206;&#x652F;&#x53EF;&#x4EE5;&#x88AB;&#x7F29;&#x5199;&#xFF0C;&#x5C31;&#x50CF;&#x4F7F;&#x7528; <code>.negative</code> &#x66FF;&#x4EE3; <code>Int.Kind.negative.</code>&#x3002;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="19_Nested_Types.html" class="navigation navigation-prev " aria-label="Previous page: 嵌套类型">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="21_Protocols.html" class="navigation navigation-next " aria-label="Next page: 协议">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"扩展","level":"1.3.20","depth":2,"next":{"title":"协议","level":"1.3.21","depth":2,"path":"chapter2/21_Protocols.md","ref":"chapter2/21_Protocols.md","articles":[]},"previous":{"title":"嵌套类型","level":"1.3.19","depth":2,"path":"chapter2/19_Nested_Types.md","ref":"chapter2/19_Nested_Types.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter2/20_Extensions.md","mtime":"2019-07-05T01:01:08.128Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-05T02:41:17.672Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src=""></script>
    <script src=""></script>
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    

    </body>
</html>

